<template>
  <div class="jsbos-common-layout">
    <div class="jsbos-common-layout-center">
      <el-row class="jsbos-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="4">
            <el-form-item label="关键词">
              <el-input
                v-model="query.keyWord"
                placeholder="请输入编码或名称"
                clearable
                @keyup.enter.native="search"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="开始时间">
              <el-date-picker
                v-model="query.orderStartDate"
                type="daterange"
                value-format="timestamp"
                format="yyyy-MM-dd"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                @keyup.enter.native="search"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="开放类型 ">
              <el-select
                v-model="query.orderType"
                placeholder="请选择"
                clearable
                @keyup.enter.native="search"
              >
                <el-option
                  v-for="(item, index) in orderTypeOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.enCode"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                @click.native.prevent="search"
                >查询</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >重置</el-button
              >
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="jsbos-common-layout-main jsbos-flex-main">
        <div class="jsbos-common-head">
          <div></div>
          <div class="jsbos-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh JSBOS-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
            <ScreenFull isContainer />
          </div>
        </div>
        <el-table
          v-loading="listLoading"
          :data="list"
          @sort-change="sortChange"
        >
          <el-table-column
            prop="orderName"
            label="调查标题"
            width="0"
            align="left"
          />

          <el-table-column
            prop="orderType"
            label="开放类型 "
            width="0"
            align="left"
          />
          <!-- <el-table-column prop="orderRange" label="内部范围" width="0" align="left" /> -->
          <el-table-column
            prop="orderStartDate"
            label="时间范围"
            width="170"
            align="center"
          >
            <template slot-scope="scope">
              {{ scope.row.orderStartDate + "—" + scope.row.orderEndDate }}
            </template>
          </el-table-column>
          <el-table-column
            prop="description"
            label="调查描述"
            width="0"
            align="left"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="creatorUserId"
            label="创建用户"
            width="0"
            align="left"
          />
          <el-table-column
            prop="creatorTime"
            label="创建时间"
            width="0"
            align="left"
          />
          <el-table-column label="操作" width="150" fixed="right">
            <template slot-scope="scope">
              <el-button
                type="text"
                v-if="!scope.row.completed && !scope.row.continueWrite"
                @click="startAnswering(scope.row)"
                >填报
              </el-button>
              <el-button
                type="text"
                v-if="scope.row.continueWrite"
                @click="startAnswering(scope.row)"
                >继续填报
              </el-button>
              <el-button
                type="text"
                v-if="!!scope.row.completed"
                @click="viewResult(scope.row)"
                >查看答案
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <Answering
          v-if="answeringVisible"
          ref="answering"
          @refresh="refreshList"
        />
        <ViewResult
          v-if="viewResultVisible"
          ref="viewResult"
          @refresh="refreshList"
        />
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import Answering from "./AnsweringQuestion.vue";
import ViewResult from "./ViewResults.vue";
import { isEmpty } from "@/utils/isEmpty";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
export default {
  components: { Answering, ViewResult },
  data() {
    return {
      showAll: false,
      orderTypeOptions: [],
      answeringVisible: false,
      viewResultVisible: false,
      query: {
        orderType: "",
        questionnaireId: undefined,
        orderName: undefined,
        keyWord: undefined,
        orderCode: undefined,
        orderStartDate: undefined,
        orderEndDate: undefined,
        enabledMark: undefined,
      },
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatorTime",
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [
        { prop: "questionnaireId", label: "调查模板" },
        { prop: "orderName", label: "调查标题" },
        { prop: "orderCode", label: "调查编码" },
        { prop: "orderRange", label: "内部包含范围" },
        { prop: "orderStartDate", label: "开始时间" },
        { prop: "orderEndDate", label: "结束时间" },
        { prop: "enabledMark", label: "有效标志" },
        { prop: "description", label: "调查描述" },
        { prop: "creatorUserId", label: "创建用户" },
        { prop: "creatorTime", label: "创建时间" },
      ],
      enabledMarkOptions: [
        { fullName: "有效", id: "1" },
        { fullName: "无效", id: "0" },
      ],
      enabledMarkProps: { label: "fullName", value: "id" },
    };
  },
  computed: {
    menuId() {
      return this.$route.meta.modelId || "";
    },
  },
  created() {
    this.initData();
    this.getOrderTypeOptions();
  },
  methods: {
    refreshList() {
      this.answeringVisible = false;
      this.viewResultVisible = false;
      this.search();
    },
    //查看答案详情
    viewResult(row) {
      this.viewResultVisible = true;
      this.$nextTick(() => {
        this.$refs.viewResult.init(row);
      });
    },
    //开始答题
    startAnswering(row) {
      this.answeringVisible = true;
      this.$nextTick(() => {
        this.$refs.answering.init(row);
      });
    },
    getOrderTypeOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "questionnaireOpeningType",
        })
        .then((res) => {
          this.orderTypeOptions = res;
        });
    },
    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == "ascending" ? "asc" : "desc";
      this.listQuery.sidx = !order ? "" : prop;
      this.initData();
    },
    initData() {
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      request({
        url: `/api/questionnaire/OqQuestionnaireOrder/getOrderList`,
        method: "post",
        data: _query,
      }).then((res) => {
        var _list = [];
        if (res.msg !== "empty") {
          for (let i = 0; i < res.data.list.length; i++) {
            let _data = res.data.list[i];
            _list.push(_data);
          }
          this.list = _list;
          this.total = res.data.pagination.total;
          this.listLoading = false;
        } else {
          this.list = [];
          this.total = 0;
          this.listLoading = false;
        }
      });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          request({
            url: `/api/questionnaire/OqQuestionnaireOrder/${id}`,
            method: "DELETE",
          }).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatorTime",
      };
      this.initData();
    },
    refresh(isrRefresh) {
      this.formVisible = false;
      if (isrRefresh) this.reset();
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
      this.search();
    },
  },
};
</script>
